<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import Edit from "./../edit/index";
import codeLibrary from "../codeLibrary/index.js";
import handoutDialog from "../handoutDialog/index.js";

export default {
  mixins: [
    Vue.prototype.$vFN.Mixins.Base,
    Vue.prototype.$vFN.Mixins.Page,
    Vue.prototype.$vFN.Mixins.Table,
  ],
  components: {},
  data() {
    return {
      appWindow: window,
      type: "",
      // 页面接口地址
      apiUrl: {
        list: "api/coupon/pageCoupon",
        stop: "api/coupon/stopActivity", // 停止发放优惠券
        start: "api/coupon/startActivity", // 开始发放优惠券
        handOut: "api/coupon/batchMemberCoupon", //主动给客户发优惠券
        delete: "api/coupon/deleteCoupon",
      },
      // 表格查询数据
      mixinTable_tableSearchData: {
        couponName: "", //券名称
        couponType: "", //券类型
        isOpen: "",
      },
      // 表格数据
      // tableData: [],
      // 表格数据删除展示结构
      tableDeleteConfig: {
        columns: [
          {
            label: "标签名称",
            prop: "couponName",
            type: "",
          },
          {
            label: "库存",
            prop: "stock",
            type: "",
          },
        ],
      },

      usersIds: [],
    };
  },
  methods: {
    // 开始/停止发放
    async distribution(item) {
      this.$confirm("确定进行此操作吗？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: "error",
        dangerouslyUseHTMLString: true,
      })
        .then(async () => {
          let that = this;
          let url_ = "";
          if (item.isOpen == 1) {
            url_ = that.apiUrl.stop;
          } else if (item.isOpen == 0) {
            url_ = that.apiUrl.start;
          }

          let res = await that.$vFN.Http.post(
            {
              url: url_,
              data: {
                ids: [item.id],
              },
            },
            window
          );
          if (res._ok) {
            that.mixinTable_refreshData();
            that.$vFN.Tools.messageSuccess("操作成功");
            that.editBoxClose();
          }
        })
        .catch(() => {});
    },

    // 打开添加/修改
    editBoxOpen(params_) {
      Edit.init({
        mixinEdit_parent: this,
        mixinEdit_editType: params_.type,
        id: params_.id,
      });
    },

    // 码库
    async toCodeLibrary(id) {
      codeLibrary.init({
        couponId: id,
        onSuccess: (res) => {},
      });
    },
    // 初始化
    async init() {
      this.mixinTable_refreshData();
    },

    handOutBoxOpen() {
      if (this.mixinTable_selectTableData <= 0) {
        this.$vFN.Tools.messageError("至少选择一种券");
        return;
      }
      console.log(this.mixinTable_selectTableData);
      let list_ = this.mixinTable_selectTableData.map((item) => {
        return {
          couponId: item.id,
          number: 0,
        };
      });

      handoutDialog.init({
        couponList: list_,
        onSuccess: (res) => {
          console.log(res, "res");
        },
      });
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>